﻿
using Maticsoft.BLL;
using Maticsoft.DBUtility;
using Maticsoft.Model;

using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Wuqi.Webdiyer;

public partial class WebPage_Youhui_Daijinquan : SystemBBasePage
{

    private Maticsoft.BLL.Card card = new Maticsoft.BLL.Card();

    private Maticsoft.Model.Partner item1 = new Maticsoft.Model.Partner();

    private int pageIndex = Convert.ToInt32(PageIndex.pageIndex);
    private Maticsoft.BLL.Partner part = new Maticsoft.BLL.Partner();
    private string sql = " 1=1 ";

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        this.getZhuangtai();
    }

    private void cardremove(string id)
    {
        this.card.Delete(id);
        base.SetSuccess("删除成功!");
        base.Response.Redirect("Youhui_Daijinquan.aspx", true);
    }

    private void downCard(string teamid, string partnerid, string code, string state)
    {
        StringBuilder builder = new StringBuilder();
        builder.Append("<html>");
        builder.Append("<meta http-equiv=content-type content=\"text/html; charset=UTF-8\">");
        builder.Append("<body><table border='1'>");
        builder.Append("<tr>");
        builder.Append("<td>ID</td>");
        builder.Append("<td>编号</td>");
        builder.Append("<td>代号</td>");
        builder.Append("<td>面额</td>");
        builder.Append("<td>实际抵用金额</td>");
        builder.Append("</tr>");
        string strWhere = " 1=1 ";
        if (teamid != "")
        {
            strWhere = strWhere + " and Team_id=" + teamid;
        }
        if (partnerid != "")
        {
            strWhere = strWhere + " and Partner_id=" + partnerid;
        }
        if (code != "")
        {
            strWhere = strWhere + " and Code='" + partnerid + "'";
        }
        if (state != "")
        {
            strWhere = strWhere + " and consume='" + state + "'";
        }
        DataSet list = new Maticsoft.BLL.Card().GetList(strWhere);
        if ((list.Tables.Count > 0) && (list.Tables[0].Rows.Count > 0))
        {
            for (int i = 0; i < list.Tables[0].Rows.Count; i++)
            {
                DataRow row = list.Tables[0].Rows[i];
                builder.Append("<tr>");
                builder.Append("<td>" + ((i + 1)).ToString() + "</td>");
                builder.Append("<td>" + row["id"].ToString() + "</td>");
                builder.Append("<td>" + row["Code"].ToString() + "</td>");
                builder.Append("<td>" + row["Credit"].ToString() + "</td>");
                Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
                if (((row["Order_id"] == null) || (row["Order_id"].ToString() == "")) || (int.Parse(row["Order_id"].ToString()) == 0))
                {
                    builder.Append("<td>未使用</td>");
                }
                else
                {
                    Maticsoft.Model.Order model = order.GetModel(int.Parse(row["Order_id"].ToString()));
                    float num2 = float.Parse(row["Credit"].ToString());
                    float num3 = float.Parse(model.Price.ToString());
                    if (num3 >= num2)
                    {
                        builder.Append("<td>" + num2 + "</td>");
                    }
                    else
                    {
                        builder.Append("<td>" + ((num2 - num3)).ToString() + "</td>");
                    }
                }
                builder.Append("</tr>");
            }
        }
        builder.Append("</table></body></html>");
        base.Response.ClearHeaders();
        base.Response.Clear();
        base.Response.Expires = 0;
        base.Response.Buffer = true;
        base.Response.AddHeader("Accept-Language", "zh-tw");
        base.Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("card_" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls", Encoding.UTF8));
        base.Response.ContentType = "Application/octet-stream";
        base.Response.Write(builder.ToString());
        base.Response.End();
    }

    private void getZhuangtai()
    {
        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        builder.Append("<tr >");
        builder.Append("<th width='120'>ID</th>");
        builder.Append("<th width='40'>面额</th>");
        builder.Append("<th width='120'>代号</th>");
        builder.Append("<th width='80'>有效期限</th>");
        builder.Append("<th width='100'>状态</th>");
        builder.Append("<th width='380'>商户名称</th>");
        builder.Append("<th width='40'>操作</th>");
        builder.Append("</tr>");
        int count = 0;
        DataTable table = DbHelperSQL.GetDataTableByPager(this.pageIndex, this.AspNetPager1.CurrentPageIndex, "Begin_time desc", this.sql, "card", out count);
        if (table.Rows.Count > 0)
        {
            this.AspNetPager1.RecordCount = count;
            for (int i = 0; i < table.Rows.Count; i++)
            {
                if ((i % 2) != 0)
                {
                    builder2.Append("<tr>");
                }
                else
                {
                    builder2.Append("<tr class='alt'>");
                }
                builder2.Append("<td>" + table.Rows[i]["Id"] + "</td>");
                builder2.Append("<td nowrap>" + table.Rows[i]["Credit"] + "</td>");
                builder2.Append("<td nowrap>" + table.Rows[i]["Code"] + "</td>");
                builder2.Append(string.Concat(new object[] { "<td nowrap>", table.Rows[i]["Begin_time"], "<br/>", table.Rows[i]["End_time"], "</td>" }));
                string str = table.Rows[i]["consume"].ToString();
                if (str == null)
                {
                    goto Label_024A;
                }
                if (!(str == "Y"))
                {
                    if (str == "N")
                    {
                        goto Label_023C;
                    }
                    goto Label_024A;
                }
                builder2.Append("<td nowrap>已用</td>");
                goto Label_0258;
            Label_023C:
                builder2.Append("<td nowrap>未用</td>");
                goto Label_0258;
            Label_024A:
                builder2.Append("<td nowrap></td>");
            Label_0258:
                if ((table.Rows[i]["Partner_id"] != null) && (table.Rows[i]["Partner_id"].ToString() != ""))
                {
                    this.item1 = this.part.GetModel(Convert.ToInt32(table.Rows[i]["Partner_id"].ToString()));
                    if (this.item1 != null)
                    {
                        builder2.Append("<td>" + this.item1.Username + "</td>");
                    }
                    else
                    {
                        builder2.Append("<td>暂未商户</td>");
                    }
                }
                else
                {
                    builder2.Append("<td>暂未商户</td>");
                }
                builder2.Append("<td class='op' nowrap>");
                builder2.Append(string.Concat(new object[] { "<a class=\"remove-record\" href='Youhui_Daijinquan.aspx?cardremove=", table.Rows[i]["Id"], "&pageIndex=", this.AspNetPager1.CurrentPageIndex, "&where=", DESEncrypt.Encrypt(this.sql, "type"), "'>删除</a>" }));
                builder2.Append("</td>");
                builder2.Append("</tr>");
            }
        }
        this.Literal1.Text = builder.ToString();
        this.Literal2.Text = builder2.ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.HeaderControl1.Select = "youhuiquan";
        if (!base.IsPostBack && (base.Request.QueryString["cardremove"] != null))
        {
            this.cardremove(base.Request.QueryString["cardremove"].ToString());
            if ((base.Request.QueryString["pageIndex"] != null) && (base.Request.QueryString["pageIndex"].ToString() != ""))
            {
                this.AspNetPager1.CurrentPageIndex = Convert.ToInt32(base.Request.QueryString["pageIndex"].ToString());
            }
            if ((base.Request.QueryString["where"] != null) && (base.Request.QueryString["where"].ToString() != ""))
            {
                this.sql = DESEncrypt.Decrypt(base.Request.QueryString["where"].ToString(), "type");
            }
        }
        if (base.Request.HttpMethod == "POST")
        {
            if (base.Request.Form["buttontype"].ToString() == "筛选")
            {
                if ((((this.txtteamid.Text != "") || (this.txtparent.Text != "")) || (this.txtcode.Text != "")) || (this.ddlstate.SelectedValue != ""))
                {
                    if (this.txtteamid.Text != "")
                    {
                        this.sql = this.sql + " and Team_id=" + this.txtteamid.Text + " ";
                    }
                    if (this.txtparent.Text != "")
                    {
                        this.sql = this.sql + " and Partner_id=" + this.txtparent.Text + " ";
                    }
                    if (this.txtcode.Text != "")
                    {
                        this.sql = this.sql + " and  code='" + this.txtcode.Text + "' ";
                    }
                    if (this.ddlstate.SelectedValue != "")
                    {
                        this.sql = this.sql + " and consume='" + this.ddlstate.SelectedValue + "' ";
                    }
                }
                else
                {
                    this.sql = " 1=1 ";
                }
            }
            if (base.Request.Form["buttontype"].ToString() == "下载")
            {
                string text = this.txtteamid.Text;
                string partnerid = this.txtparent.Text;
                string code = this.txtcode.Text;
                string selectedValue = this.ddlstate.SelectedValue;
                this.downCard(text, partnerid, code, selectedValue);
            }
        }
        this.getZhuangtai();
    }
}

